package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Picture;
import com.weipai.springboot.model.PictureCategory;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.PictureDao;
import com.weipai.springboot.module.service.PictureService;
import com.weipai.springboot.module.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/27 17:27
 */
@Service
public class PictureServiceImpl implements PictureService{

    private static final Logger log = LoggerFactory.getLogger(PictureServiceImpl.class);

    @Autowired
    private PictureDao pictureDao;
    
    //通过图片的分类获取图片
    @Override
    public Response getImgByPictureCategoryId(String id) {
        Response response = new Response();
        List<Map<String, Object>> map  =pictureDao.getImgByPictureCategoryId(id);
        if(null!=map && map.size()>0){
            response.setCode(RetCode.SUCCESS);
            response.put("imgList",map);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //图片的保存
    @Override
    public Response savePicture(Picture picture) {
        Response response = new Response();
        picture.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
        if(pictureDao.savePicture(picture)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //通过图片id获取图片详情
    @Override
    public Response getPictureById(Integer id) {
        Response response = new Response();
        Picture picture = pictureDao.getPictureById(id);
        if(null!= picture){
            response.setCode(RetCode.SUCCESS);
            response.put("picture",picture);
        }else{
            log.info("未请求到图片数据，图片id：{}"+id);
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //图片的删除
    @Override
    public Response batchDeleteById(Integer[] id) {
        Response response = new Response();
        if(pictureDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //图片的修改
    @Override
    public Response updatePicture(Picture picture) {
        Response response = new Response();
        picture.setUpdateTime(DateUtils.timeFormat(System.currentTimeMillis()));
        if(pictureDao.updatePicture(picture)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //图片分类的保存
    @Override
    public Response savePictureCategory(PictureCategory pictureCategory) {
        Response response = new Response();
        String shopId = "1";
        pictureCategory.setShopId(shopId);
        if(pictureDao.savePictureCategory(pictureCategory)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //图片分类的修改
    @Override
    public Response updatePictureCategory(Integer id, String categoryName) {
        Response response = new Response();
        if(pictureDao.updatePictureCategory(id, categoryName)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //获取图片分类的详情
    @Override
    public Response getPictureCategoryInfo(Integer id) {
        Response response = new Response();
        Map<String, Object> map = pictureDao.getPictureCategoryInfo(id);
        if(null!=map && map.size()>0){
            response.setCode(RetCode.SUCCESS);
            response.setData(map);
        }else{
            log.info("未请求到图片分类数据，图片分类id：{}"+id);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //图片分类的删除
    @Override
    public Response batchDeleteByCategoryId(Integer[] id) {
        Response response = new Response();
        int size = id.length;
        for(int i=0;i<size;i++){
            if(pictureDao.vertifiPicture(id[i])>0){
                response.setCode(RetCode.ASSOCIATED_DATA);
                return response;
            }
        }
        if(pictureDao.batchDeleteByCategoryId(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //图片分类列表
    @Override
    public Response pictureCategoryList() {
        Response response = new Response();
        List<PictureCategory> pictureCategoryList =pictureDao.pictureCategoryList();
        if(!pictureCategoryList.isEmpty()){
            response.setCode(RetCode.SUCCESS);
            response.setData(pictureCategoryList);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }
}
